package tech.yunland.shop.domain.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.moyuer.bsf.domain.model.User;

import javax.persistence.*;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 充值单
 *
 * @author: 袁首京<yuanshoujing@gmail.com> 16/4/13
 */
@Entity
@Table(name = "bill_charge")
public class ChargeBill extends Bill {

	@JSONField(serialize = false)
	@ManyToOne
	@NotFound(action = NotFoundAction.IGNORE)
	private User member;

	@OneToMany(mappedBy = "owner")
	private Set<ChargeBillEntry> entries; // 分录

	@Column(nullable = false)
	private BigDecimal receivable; // 应收

	@Column(nullable = false)
	private BigDecimal received; // 实收

	@Column(name = "handlers_id")
	private String handlers; // 经手人(销售员)



	@Column(name = "payment")

	private Integer payment;//支付方式
	@Transient
	private String memberId;
	@Transient
	private String memberName;
	@Transient
	private String packageName;//优惠活动名称
	@Transient
	private BigDecimal packageQuantity; //优惠活动数量
	public Integer getPayment() {
		return payment;
	}

	public void setPayment(Integer payment) {
		this.payment = payment;
	}
	public String getPackageName() {
		if(this.packageName!=null){
			return this.packageName;
		}
		if(this.entries!=null){
			List names=new ArrayList<>();
			for(ChargeBillEntry chargeBillEntry:this.entries){
				names.add(chargeBillEntry.getGoodsPackage().getName());
			}
			this.packageName=StringUtils.join(names,",");
		}
		return packageName;
	}
	
	
	public void setPackageName(String packageName) {
		this.packageName = packageName;
	}

	public BigDecimal getPackageQuantity() {
		if(this.entries!=null){
			for(ChargeBillEntry chargeBillEntry:this.entries){
				BigDecimal quantity = chargeBillEntry.getQuantity();
				this.packageQuantity=quantity;
			}
		}
		return packageQuantity;
	}

	public void setPackageQuantity(BigDecimal packageQuantity) {
		this.packageQuantity = packageQuantity;
	}

	public User getMember() {
		return member;
	}

	public void setMember(User member) {
		this.member = member;
	}

	public Set<ChargeBillEntry> getEntries() {
		return entries;
	}

	public void setEntries(Set<ChargeBillEntry> entries) {
		this.entries = entries;
	}

	public BigDecimal getReceivable() {
		return receivable;
	}

	public void setReceivable(BigDecimal receivable) {
		this.receivable = receivable;
	}

	public BigDecimal getReceived() {
		return received;
	}

	public void setReceived(BigDecimal received) {
		this.received = received;
	}

	public String getHandlers() {

		return handlers;
	}

	public void setHandlers(String handlers) {
		this.handlers = handlers;
	}

	public String getMemberId() {
		if (this.memberId != null) {
			return this.memberId;
		}

		if (this.member != null) {
			this.memberId = this.member.getId();
		}

		return this.memberId;
	}

	public void setMemberId(String memberId) {
		this.memberId = memberId;
	}

	public String getMemberName() {
		if (this.memberName != null) {
			return this.memberName;
		}

		if (this.member != null) {
			this.memberName = this.member.getName();
		}

		return this.memberName;
	}

	public void setMemberName(String memberName) {
		this.memberName = memberName;
	}
}
